// Pedro Ferrer (gth817g)

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.CCT
{
    public class ProgressBar : IProgressBar, IComponent
    {
        protected Rectangle mainRectangle;
        protected Rectangle stepRectangle;
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush mainBrush = new SolidBrush(Color.White);
        protected SolidBrush stepBrush = new SolidBrush(Color.Gold);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected string text;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected int max = 100;
        protected int min = 0;
        protected int val = 0;
        protected int step = 1;


        #region Properties
        public Point Location
        {
            get { return mainRectangle.Location; }
            set { mainRectangle.Location = value; Invalidate(); }
        }

        public Rectangle Bounds
        {
            get { return mainRectangle; }
            set { mainRectangle = value; Invalidate(); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return mainBrush.Color; }
            set { mainBrush.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        public event EventHandler KeyDown;
        #endregion


        public ProgressBar()
        {
            mainRectangle = new Rectangle(0, 0, 150, 25);
            labelFormat.LineAlignment = StringAlignment.Center;
            labelFormat.Alignment = StringAlignment.Center;
        }

        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                stepRectangle = new Rectangle(mainRectangle.Location.X, mainRectangle.Location.Y, (val * mainRectangle.Width / max), 25);
                g.FillRectangle(mainBrush, mainRectangle);
                g.FillRectangle(stepBrush, stepRectangle);
                g.DrawRectangle(Pens.Blue, mainRectangle.Left, mainRectangle.Top, mainRectangle.Width, mainRectangle.Height);
                g.DrawRectangle(Pens.LightBlue, mainRectangle.Left - 1, mainRectangle.Top - 1, mainRectangle.Width + 2, mainRectangle.Height + 2);
                double percent = (double)val /(double)max;
                g.DrawString(String.Format("{0:0%}", percent), font, Brushes.Blue, (RectangleF)mainRectangle, labelFormat);
                
                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e) 
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) 
        {
            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }

        }


        #region IProgressBar Members

        public int Minimum
        {
            get
            {
                return min;
            }
            set
            {
   
                if (value < 0)                
                    min = 0;
                
                if (value > max)                
                    min = value;
               
                if (val < min)
                    val = min;
                
                Invalidate();
            }
        }

        public int Maximum
        {
            get
            {
                return max;
            }
            set
            {
                if (value<min)
                    min=value;

                max = value;

                if (val > max)
                    val = max;
                
                Invalidate();
            }
        }

        public int Step
        {
            get
            {
                return step;
            }
            set
            {
                step = value;

                if (step < 1)
                    step = 1;
                
                if (step < (max - val))
                    step = (max - val);
                
                Invalidate();
                
            }
        }

        public int Value
        {
            get
            {
                return val;
            }
            set
            {
                if (value < min)
                    val = min;
                
                else if (value > max)
                    val = max;
                
                else
                    val = value;
                Invalidate();
            }
        }

        public void PerformStep()
        {
            val += step;

            if (val > max)
                val = max;

            Invalidate();
        }

    
        #endregion


    }
}


   